package com.demo2do.fms.admin.web.controller.business;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.business.ReimburseSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.business.BalanceService;
import com.demo2do.fms.admin.service.business.ShipmentService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.business.shipment.ShipmentOrder;

/**
 * 报销
 * 
 * @author Ark
 *
 */
@Controller
@RequestMapping("/reimburses")
@MenuSetting("menu-business")
public class ReimburseController {
    
    private static final Logger logger = LoggerFactory.getLogger(ReimburseController.class);
    
    @Autowired
    private BalanceService balanceService;
    
    @Autowired
    private ShipmentService shipmentService;

    /**
     * 报销 - search page
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-reimburse")
    public ModelAndView reimburseSearch(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("business/reimburse/reimburse-search");
        
        modelAndView.addObject("region", login.isRegionUser());
        modelAndView.addObject("orgIds", login.getCurrOrgIds());
        
        return modelAndView;
        
    }
    
    /**
     * 报销 - on search
     * 
     * @param search
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "expense")
    @MenuSetting("submenu-reimburse")
    public ModelAndView onReimburseSearch(ReimburseSearch search, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("business/reimburse/reimburse-list");
        
        int totals = balanceService.countReimburse(search);
        if (totals > 0) {
            
            logger.warn("#onReimburseSearch() - search order, size=[{}]", totals);
            
            List<Map<String, Object>> result = balanceService.listReimburse(search);
            
            modelAndView.addObject("result", result);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 报销 - 回单确认勾选列表页
     * 
     * @param dispatchOrderIds
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    @MenuSetting("submenu-reimburse")
    public ModelAndView reimburse(@RequestParam(name="dispatchOrderIds") List<Long> dispatchOrderIds) {
        
        ModelAndView modelAndView = new ModelAndView("business/reimburse/reimburse-order-list");
        
        List<ShipmentOrder> shipmentOrders = shipmentService.listShipmentOrder(dispatchOrderIds);
        
        modelAndView.addObject("shipmentOrders", shipmentOrders);
        modelAndView.addObject("dispatchOrderIds", dispatchOrderIds);
        
        return modelAndView;
        
    }
    
    /**
     * 报销
     * 
     * @param login
     * @param dispatchOrderIds
     * @param shipmentOrderItems
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @MenuSetting("submenu-reimburse")
    public @ResponseBody HttpResult onReimburse(@Secure Login login, 
                                                @RequestParam(name="dispatchOrderIds") List<Long> dispatchOrderIds,
                                                @RequestParam(name="shipmentOrderItems", required = false) List<String> shipmentOrderItems) {

        HttpResult httpResult = new HttpResult();
        
        try {
            
            logger.warn("#onReimburse() - reimburse order, size=[{}]", dispatchOrderIds.size());
            
            // do reimburse
            balanceService.reimburse(dispatchOrderIds, shipmentOrderItems, login);
            
            httpResult.success().message("报销成功");
            
        } catch (Exception e) {
            logger.error("#onReimburse() expense fail, exception is [{}]", e);
            httpResult.fail("500").message("报销失败！ " + e.getMessage());
        }
        
        return httpResult;
        
    }
    
}